<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Permission;

class Base extends Controller
{
    protected $uid;
    protected $username;
    protected $role_id;
    protected $per_model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->per_model = new Permission();

        //判断是否登陆
        if (!session('uid')) {
            $this->error('请登陆', 'admin/login/index');
        }

        $this->uid = session('uid');
        $this->username = session('username');
        $this->role_id = session('role_id');

        // //菜单数据(从权限表获取数据), 并分级
        $data_menu = $this->per_model->where('menu=1')->field('id, pid, name, method')->select();

        $data_menu = obj_tree($data_menu);

        // echo json_encode($data_menu);
        // exit;
        
        //当前活动菜单id
        $controller = $request->controller();
        // var_dump($controller);
        // exit;
        // $menu_url = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $controller)).'/index';
        // 权限表, 找对应的 method,不用加index
        $method = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $controller));
        
        $smid = $this->per_model->field('id, pid')->where('method', $method)->find();

        //当前控制器及方法(转小写)
        $controller = $method;
        
        $action = strtolower($request->action());

        // //权限检查
        // if (!$this->_checkAuthor($controller, $action)) {
        //     $this->error('你无权限进行此操作', 'index/index');
        // }

        // 记录日志
        $this->_addLog();

        $this->assign('data_menu', $data_menu);
        $this->assign('smid', $smid->id);
        $this->assign('pmid', $smid->pid);
        $this->assign("controller", $controller);
        $this->assign("action", $action);
    }

    /**
     * 权限检查
     */
    private function _checkAuthor($c, $a)
    {
        $rid = $this->role_id;
        
        if (!$rid) {
            return false;
        }
        
        if ($rid==1) {
            return true;
        }
     
        if (preg_match('/^public_/', $a)) {
            return true;
        }

        if ($c == 'index' && $a == 'index') {
            return true;
        }
        
        $pids = model('admin_role')->where('id', $rid)->value('powers');
        
        $powers = model('powers')->field('id, pid, method')->select($pids);

        $arr = array();

        foreach ($powers as $val) {
            if ($val->pid==0) {
                $arr[] = $val->method.'/index';
            } else {
                $arr[] = model('powers')->where('id', $val->pid)->value('method').'/'.$val->method;
            }
        }

        if (in_array($c.'/'.$a, $arr)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 记录日志
     */
    private function _addLog()
    {
        $data = array();
        $data['querystring'] = request()->query()?'?'.request()->query():'';
        $data['m'] = request()->module();
        $data['c'] = request()->controller();
        $data['a'] = request()->action();
        $data['userid'] = $this->uid;
        $data['username'] = $this->username;
        $data['ip'] = ip2long(request()->ip());
        $data['time'] = time();
        $arr = array('Index/index','Log/index','Menu/index');
        if (!in_array($data['c'].'/'.$data['a'], $arr)) {
            db('admin_log')->insert($data);
        }
    }
}
